package com.example.park.addpark

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.core.widget.addTextChangedListener
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.ParkBeanItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.park.R
import com.example.park.databinding.ActivityParkAddBinding
import com.example.park.park.ParkViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/park/ParkAddActivity")
class ParkAddActivity : BaseActivity<ActivityParkAddBinding,ParkViewModel>() {
    var  area ="A区停车场"
    var type="标准车位"
    var areaId =1
    var status="空闲"
    override fun initView() {
        //添加
        binding.parkaddAdd.setOnClickListener {
            val carNo =binding.parkaddCarNo.text.toString()
            val remark =binding.parkaddRemark.text.toString()
            val item =ParkBeanItem(areaId,area, null,"",remark,carNo,status,type,0,"")
            vm.addSuccess(item)
        }
        binding.parkaddRemark.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                val length = p0?.length
                binding.parkaddNum.setText("${length}/10")
                if (length==10){
                    binding.parkaddNum.setTextColor(Color.RED)
                }else{
                    binding.parkaddNum.setTextColor(Color.DKGRAY)
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }

        })

        val areas = arrayOf("A区停车场", "B区停车场", "C区停车场")
        val areaAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, areas)
        areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddArea.adapter=areaAdapter
        binding.parkaddArea.onItemSelectedListener=object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                area=areas[p2]
                areaId=p2+1
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }
        }
        val types = arrayOf("标准车位","VIP车位")
        val typeAdapter =ArrayAdapter(this,android.R.layout.simple_spinner_item,types)
        typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddType.adapter =typeAdapter
        binding.parkaddType.onItemSelectedListener =object :OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                type=types[p2]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }
        binding.parkaddRg.setOnCheckedChangeListener { radioGroup, checkedId ->
            when(checkedId){
                R.id.pardadd_kx->status="空闲"
                R.id.pardadd_zy->status="已占用"
                else->status="维修中"
            }
        }
    }

    override fun initData() {
        vm.addCarSuccess.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
    }

}